import React from 'react';



 
interface IProps {
    children?: JSX.Element,
}

/**
 * js 报错页
 * @returns 
 */
export default function JSErrorPage(props: IProps) {

    const { children } = props;
    /** 是否存在js报错 */
    const [hasError, setHasError] = React.useState(false);
    /** 保存数据 */
    const [errorMessage, setErrorMessage] = React.useState('');

    React.useEffect(() => {
        window.addEventListener('error', (event)=>{
            console.log(event);
            setHasError(true);
            setErrorMessage(event.message);
        }, {once: true});
    }, [])

    return (
        hasError ? <>{errorMessage}</> : children
    )
}
